package de.tabuma.sso.group;

import de.tabuma.customer.entity.*;
import de.tabuma.customer.logik.CustomerUserRelationFacade;
import de.tabuma.sso.logik.*;
import de.tabuma.sso.groupprivileg.*;
import de.tabuma.sso.usergroup.*;

import java.util.*;
import java.util.logging.*;
import de.tabuma.lib.contentmanagement.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class GroupFacade implements FacadeInterface {

    private static final Logger log = Logger.getLogger(GroupFacade.class.getName());

    public static int create(Group group, String user) {
        return (int) PMF.doCreate(group, user);
    }

    public int createElement(Object object, String user) {
        return create((Group) object, user);
    }

    public static void update(Group group, String user) {
        PMF.doUpdate(group, user);
    }

    public void updateElement(Object object, String user) {
        update((Group) object, user);
    }

    public static Group get(int id) {
        Group group = PMF.get(id, new Group());
        if(group.getAbleToCreateGroupWithNoLicenseNeeded()==null) {
            group.setAbleToCreateGroupWithNoLicenseNeeded(Boolean.FALSE);
        }
        if(group.getNoLicenseNeeded()==null) {
            group.setNoLicenseNeeded(Boolean.FALSE);
        }
        return group;
    }

    public Object getElement(int id) {
        return get(id);
    }

    public static void remove(int id, String user) {
        Group group = get(id);
        PMF.doRemove(group, user);

        List<GroupPrivileg> gpList = GroupPrivilegFacade.getList(id);
        for(int i=0;i<gpList.size();i++) {
            GroupPrivileg gp = gpList.get(id);
            GroupPrivilegFacade.remove(gp.getId().intValue(), user);
        }

        List<UserGroup> ugList = UserGroupFacade.getList(id);
        for(int i=0;i<ugList.size();i++) {
            UserGroup ug = ugList.get(i);
            UserGroupFacade.remove(ug.getId().intValue(), user);
        }
    }

    public void removeElement(int id, String user) {
        remove(id, user);
    }

    public static List<Group> getList(int customerId) {
        String keys[] = new String[1];
        keys[0] = "customerId";
        String values[] = new String[1];
        values[0] = "" + customerId;
        List<Group> gList = PMF.get(keys, values, new Group());
        for(int i=0;i<gList.size();i++) {
            Group g = gList.get(i);
            if(g.ableToCreateGroupWithNoLicenseNeeded==null) {
                g.ableToCreateGroupWithNoLicenseNeeded = false;
            }
            if(g.noLicenseNeeded==null) {
                g.noLicenseNeeded = false;
            }
            gList.set(i, g);
        }
        return gList;
    }

    public static List<Group> getList(String user) {
        List<CustomerUserRelation> curList = CustomerUserRelationFacade.getList(user);
        List<Group> list = new ArrayList<Group>();
        for(int i=0;i<curList.size();i++) {
            List<Group> gList = getList(curList.get(i).getCostumerId());
            list.addAll(gList);
        }
        return list;
    }

    public List getElementList(String listParameter) {
        return getList(listParameter);
    }
    
}